﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ScreenMonitor.Common.Objects
{
    /// <summary>
    /// 为信息发送提供包装
    /// </summary>
    public sealed class SendMessageObject
    {
        private static object StaticLockObj = new object();
        private object LockObj = new object();

        /// <summary>
        /// 消息标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 消息内容
        /// </summary>
        public string  Message { get; set; }

        public byte[] ToByte()
        {
            lock (LockObj)
            {
                MemoryStream stream = new MemoryStream();
                BinaryWriter bw = new BinaryWriter(stream);
                bw.Write(Title);
                bw.Write(Message);
                bw.Flush();
                byte[] r = stream.ToArray();
                bw.Close();
                return r;
            }
        }

        public static SendMessageObject FromByte(byte[] Data)
        {
            lock (StaticLockObj)
            {
                MemoryStream stream = new MemoryStream(Data);
                BinaryReader br = new BinaryReader(stream);
                SendMessageObject so = new SendMessageObject();
                so.Title = br.ReadString();
                so.Message = br.ReadString();
                br.Close();
                return so;
            }
        }

    }
}
